﻿layui.extend({
    unobtrusive: 'lib/unobtrusive',
}).define(['table', 'form', 'layer', 'util','index','jquery','unobtrusive','permisson'], function (exports) {
    var table = layui.table,
        form = layui.form,
        index = layui.index,
        util = layui.util,
        permisson=layui.permisson,
        $=layui.$,
        layer = parent === self ? layui.layer : top.layui.layer;
    var pageName = layui.cache.module.pageName;

    var closeLayer = function () {
        var iframeIndex = layer.getFrameIndex(window.name);
        layer.close(iframeIndex);
    }

    //列表页
    if (pageName === 'indexPage') {
        
        //初始化角色表格数据
        var roleTable = table.render({
            elem: '#data_Role',
            page: true,
            url: '/Admin/RoleManage/RoleList',
            cols: [[
                { field: 'RoleId', title: '角色Id', width: '10%', align: 'center' },
                { field: 'RoleName', title: '角色名称', width: '15%' },
                { field: 'Description', title: '角色描述', width: '20%' },
                {
                    field: 'CreateTime',
                    title: '创建时间',
                    width: '15%',
                    align: 'center',
                    templet: function (data) {
                        var timestamp = parseInt(data.CreateTime.replace("/Data(", "").replace(")/", ""));
                        return util.toDateString(timestamp, "yyyy-MM-dd HH:mm");
                    }
                },
                {
                    field: 'UpdateTime',
                    title: '更新时间',
                    width: '15%',
                    align: 'center',
                    templet: function (data) {
                        var timestamp = parseInt(data.UpdateTime.replace("/Date(", "").replace(")/", ""));
                        return util.toDateString(timestamp, "yyyy-MM-dd HH:mm");
                    }
                },
                { field: 'Operate', title: '操作', toolbar: '#tpl_Operate' }
            ]],
            done: function () {
               
                permisson.controllPermisson();
            }
           
        });


        //刷新表格(表格重载)
        var refreshTable = function () {
            roleTable.reload();
        }

        //角色添加按钮点击事件
        $('#btn_AddRole').click(function () {
            layer.open({
                type: 2,
                title: '新增角色',
                area: ['640px', '400px'],
                content: '/Admin/RoleManage/Create',
                end: function () {
                    refreshTable();
                }
            });
        });

        //表格操作按钮监听事件
        table.on('tool(role)',
            function (result) {
                var roleData = result.data,
                    event = result.event;

                //角色删除事件
                if (event === 'delete') {
                    layer.confirm('确实要删除角色吗？',
                        {
                            icon: 3,
                            title: '信息提示'
                        },
                        function (layerIndex) {
                            layer.close(layerIndex);
                            var roleId = roleData.RoleId;
                            $.post('/Admin/RoleManage/Delete',
                                {
                                    roleId: roleId
                                },
                                function (rtnResult) {
                                    if (rtnResult.code !== 0) {
                                        layer.msg(rtnResult.msg);
                                    } else {
                                        refreshTable();
                                    }
                                });
                        });
                }

                if (event === 'update') {
                    layer.open({
                        type: 2,
                        title: '新增角色',
                        area: ['640px', '400px'],
                        content: '/Admin/RoleManage/Update?roleId=' + roleData.RoleId,
                        end: function () {
                            refreshTable();
                        }
                    });
                }
            });

    }


    //添加页
    if (pageName==='createPage') {
        //添加完成回调
        window.onRequestRoleAddComplete = function (res) {
            index.handleCallbackResult(res,
                function () {
                    closeLayer();
                });

        }
    }
    if (pageName==='updatePage') {
        //初始化数据
        var roleId = $('#roleId').val();
        $.post('/Admin/RoleManage/UpdateInit',
            {
                roleId:roleId
            },
            function (rtnResult) {
                if (rtnResult.code !== 0) {
                    layer.msg(rtnResult.msg);
                } else {
                    var data = rtnResult.data;
                    form.val('role',
                        {
                            Name: data.Name,
                            Description: data.Description
                        });
                }
            });

        //编辑完成回调
        window.onRequestRoleUpdateComplete = function (res) {
            index.handleCallbackResult(res,
                function () {
                    closeLayer();
                });
        }
    }


    exports('roleManage', null);
})